package com.sq.futuralsdkdemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.sq.futuralsdk.FuturalAPIUtils;
import com.sq.libcommon.bean.ActionResultBean;
import com.sq.libcommon.bean.PoisBean;
import com.sq.libcommon.util.ExecutorUtils;

import java.lang.ref.WeakReference;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private static ActionResultBean actionResultBean;

    private static MyHandler handler;
    private static final int CHECK_NAV_STATUS = 1;
    private static final int CHECK_NAV_INTERVAL = 2*1000; //检查导航状态间隔，2秒或1秒，自行决定
    static class MyHandler extends Handler {
        private WeakReference<MainActivity> reference;

        public MyHandler(MainActivity activity) {
            this.reference = new WeakReference<MainActivity>(activity);
        }

        public Activity getActivity() {
            return reference.get();
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            Log.e(TAG, "handleMessage, what: " + msg.what);
            switch (msg.what) {
                case CHECK_NAV_STATUS:
                    FuturalAPIUtils.checkAction((new FuturalAPIUtils.ActionCallBack() {
                        @Override
                        public void onResponse(ActionResultBean bean) {
                            Log.e(TAG, "checkAction, " + bean);
                            if (handler.getActivity().isDestroyed()) return;

                            //某些情况下，action id会被底盘重置，导致check ation时一直返回404，机器人就会停止在某点上，不动了，这种情况下重新导航去某点
                            if (bean.getAction_id() == -404) {
                                //请再调用一次导航去某个点的api
                            } else if (bean != null && bean.getState().getStatus() == 4 && bean.getState().getResult() == 0) {
                                //到达点位
                                Log.d(TAG, "arrived point");
                            } else {
                                //如果没有到达指定地点，请再一次检查
                                handler.sendEmptyMessageDelayed(CHECK_NAV_STATUS, CHECK_NAV_INTERVAL);
                            }
                        }
                        @Override
                        public void onFailure() {
                            handler.sendEmptyMessageDelayed(CHECK_NAV_STATUS, CHECK_NAV_INTERVAL);
                        }
                    }), actionResultBean.getAction_id());

                    break;
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        handler = new MyHandler(this);
    }

    public void goToReceptionPoint(View view) {
        ExecutorUtils.getExecutor().execute(()->{
            List<PoisBean> positions = FuturalAPIUtils.getPoisSync();
            //也可以这样使用
            //String jsonStr = FuturalUtils.getCurrentMapPointsInfo();
            //List<PoisBean> positions = JSON.parseObject(jsonStr, new TypeReference<List<PoisBean>>() {});
            for (int i = 0; i < positions.size(); i++) {
                if ("接待点".equalsIgnoreCase(positions.get(i).getMetadata().getDisplay_name())) {
                    goToPoint(positions.get(i));
                    break;
                }
            }
        });
    }

    public void goToTestPoint(View view) {
        ExecutorUtils.getExecutor().execute(()->{
            List<PoisBean> positions = FuturalAPIUtils.getPoisSync();
            //也可以这样使用
            //String jsonStr = FuturalUtils.getCurrentMapPointsInfo();
            //List<PoisBean> positions = JSON.parseObject(jsonStr, new TypeReference<List<PoisBean>>() {});
            for (int i = 0; i < positions.size(); i++) {
                if ("测试点".equalsIgnoreCase(positions.get(i).getMetadata().getDisplay_name())) {
                    goToPoint(positions.get(i));
                    break;
                }
            }
        });
    }

    private FuturalAPIUtils.ActionCallBack actionCallBack = new FuturalAPIUtils.ActionCallBack() {
        @Override
        public void onResponse(ActionResultBean bean) {
            actionResultBean = bean; //保存越来，查询导航状态时使用
        }

        @Override
        public void onFailure() {
            //在这里可以提示用户失败了
        }
    };
    private void goToPoint(PoisBean poisBean) {
        PoisBean.PoseBean poseBean = new PoisBean.PoseBean();
        poseBean.setX(poisBean.getPose().getX());
        poseBean.setY(poisBean.getPose().getY());
        poseBean.setYaw(poisBean.getPose().getYaw());
        FuturalAPIUtils.createMoveToAction(actionCallBack, poseBean);
    }

    public void goToDock(View view) {
        FuturalAPIUtils.goToCharge(this, new FuturalAPIUtils.ActionCallBack() {
            @Override
            public void onResponse(ActionResultBean bean) {
                Log.e(TAG, "send go to dock success");
            }

            @Override
            public void onFailure() {
                //在这里可以提示用户失败了
            }
        });
    }

    public void exit(View view) {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
        handler = null;
    }
}